from manimlib import*
import numpy as np
import random
#注：需要将brace.py中加入import copy

class intro(Scene):
    def construct(self):
        text=["在学习分数的时候，老师告诉我们","所有分数都能化成循环小数","但你有想过为什么吗？"]
        textvg=VGroup(*[TexText(t) for t in text]).scale(0.75).to_edge(DOWN)
        for t in textvg:
            t.insert_n_curves(20)
        textvg[1].move_to(ORIGIN).scale(2)
        #self.add_sound("bgm")
        self.play(Write(textvg[0]))
        self.wait(0.5)
        self.play(Write(textvg[1]))
        self.wait(2)
        self.play(FadeOut(textvg[0]),Write(textvg[2]))
        self.wait(2)
        self.play(FadeOut(textvg[1:]))

class long_division(Scene):
    def construct(self):
        text=["我们先来观察一下我们将分数化为小数的过程"]
        textvg=VGroup(*[TexText(t) for t in text]).scale(0.75).to_edge(DOWN)
        for t in textvg:
            t.insert_n_curves(20)
        frac=Tex("1","1","\\over","1","5")
        long=VGroup(
            VGroup(
                Tex("1").move_to(np.array([-2.25,2.25,0])),
                Tex("5").move_to(np.array([-1.75,2.25,0])),
                Tex("1").move_to(np.array([-1.25,2.25,0])),
                Tex("1").move_to(np.array([-0.75,2.25,0])),
                Line(np.array([-1.5,2.5,0]),np.array([1.5,2.5,0])),
                ArcBetweenPoints(np.array([-1.5,2.5,0]),np.array([-1.5,2,0]),-PI/3)
            ),
            VGroup(
                Tex(".","0").move_to(np.array([-0.25,2.25,0])),
                Tex(".","7").move_to(np.array([-0.25,2.75,0])),
                Tex("1").move_to(np.array([-1.25,1.75,0])),
                Tex("0").move_to(np.array([-0.75,1.75,0])),
                Tex(".","5").move_to(np.array([-0.25,1.75,0])),
                Line(np.array([-1.5,1.5,0]),np.array([0.5,1.5,0]))
            ),
            VGroup(
                Tex(".","5").move_to(np.array([-0.25,1.25,0])),
                Tex("0").move_to(np.array([0.25,1.25,0])),
                Tex("3").move_to(np.array([0.25,2.75,0])),
                Tex(".","4").move_to(np.array([-0.25,0.75,0])),
                Tex("5").move_to(np.array([0.25,0.75,0])),
                Line(np.array([-0.5,0.5,0]),np.array([1,0.5,0]))
            ),
            VGroup(
                Tex("5").move_to(np.array([0.25,0.25,0])),
                Tex("0").move_to(np.array([0.75,0.25,0])),
                Tex("3").move_to(np.array([0.75,2.75,0])),
                Tex("4").move_to(np.array([0.25,-0.25,0])),
                Tex("5").move_to(np.array([0.75,-0.25,0])),
                Line(np.array([0,-0.5,0]),np.array([1.5,-0.5,0]))
            ),
            VGroup(
                Tex("5").move_to(np.array([0.75,-0.75,0])),
                Tex(r"\ddots").move_to(np.array([1.25,-1.25,0])),
                Tex(r"\cdots").move_to(np.array([1.25,2.75,0])),
                
            )
        )
        conclusion=TexText(r"$\cdot$每次计算时，余数小于除数\\",r"$\cdot$当余数出现循环时，结果出现循环").insert_n_curves(20).scale(0.75).to_edge(LEFT)
        conclusion[0].align_to(conclusion[1],LEFT)
        conclusion[0][0].scale(2).set_color(BLUE)
        conclusion[1][0].scale(2).set_color(BLUE)
        #self.add_sound("bgm")
        self.play(Write(textvg[0]))
        self.wait(0.5)
        self.play(Write(frac))
        self.wait()
        self.play(
            FadeTransform(frac[0:2],long[0][2:4]),
            FadeTransform(frac[3:5],long[0][0:2]),
            FadeTransform(frac[2],long[0][4:6]),
        )
        self.wait()
        self.play(
            ShowCreationThenDestructionAround(long[0][2:4]),
            ShowCreationThenDestructionAround(long[0][0:2]),
        )
        self.wait(0.5)
        self.play(Write(long[1][0]))
        self.wait(0.5)
        self.play(Write(long[1][1]))
        self.wait(0.5)
        self.play(
            FadeTransform(long[0][0:2].copy(),long[1][2:-1]),
            FadeTransform(long[1][1].copy(),long[1][2:-1]),
        )
        self.wait(0.5)
        self.play(ShowCreation(long[1][-1]))
        self.wait()
        self.play(
            FadeTransform(VGroup(long[1][2:5].copy(),long[1][0].copy(),long[0][2:4].copy()),long[2][0]),
        )
        self.wait(0.5)
        self.play(Write(long[2][1]))
        self.wait(0.5)
        self.play(Write(long[2][2]))
        self.wait(0.5)
        self.play(
            FadeTransform(long[0][0:2].copy(),long[2][3:-1]),
            FadeTransform(long[2][2].copy(),long[2][3:-1]),
        )
        self.wait(0.5)
        self.play(ShowCreation(long[2][-1]))
        self.wait()
        self.play(
            FadeTransform(VGroup(long[2][3:-1].copy(),long[2][0:2].copy()),long[3][0]),
        )
        self.wait(0.5)
        self.play(Write(long[3][1]))
        self.wait(0.5)
        self.play(Write(long[3][2]))
        self.wait(0.5)
        self.play(
            FadeTransform(long[0][0:2].copy(),long[3][3:-1]),
            FadeTransform(long[2][2].copy(),long[3][3:-1]),
        )
        self.wait(0.5)
        self.play(ShowCreation(long[3][-1]))
        self.wait()
        self.play(
            FadeTransform(VGroup(long[3][3:5].copy(),long[3][0:2].copy()),long[4][0]),
        )
        self.wait(0.5)
        self.play(Write(long[4][1:]))
        self.wait()
        self.play(
            ShowCreationThenDestructionAround(long[2][0]),
            ShowCreationThenDestructionAround(long[0][0:2]),
        )
        self.play(
            ShowCreationThenDestructionAround(long[3][0]),
            ShowCreationThenDestructionAround(long[0][0:2]),
        )
        self.play(
            ShowCreationThenDestructionAround(long[4][0]),
            ShowCreationThenDestructionAround(long[0][0:2]),
        )
        self.wait(0.5)
        self.play(Write(conclusion[0]))
        self.wait()
        self.play(LaggedStart(*[ShowCreationThenDestructionAround(long[i][0]) for i in range(2,5)],lag_ratio=0.5))
        self.play(LaggedStart(*[ShowCreationThenDestructionAround(long[i][2]) for i in range(2,5)],lag_ratio=0.5))
        self.wait(0.5)
        self.play(Write(conclusion[1]))
        self.wait()
        self.play(conclusion.animate.to_corner(UL),FadeOut(textvg),FadeOut(long))
        self.wait()

class general(Scene):
    def construct(self):
        text=["假设我们要化简的是分数","那么我们可以知道余数一定小于$b$","那么总有一天小于$b$的余数会“用”完","此时余数出现循环","也就是结果出现循环"]
        textvg=VGroup(*[TexText(t) for t in text]).scale(0.75).to_edge(DOWN)
        for t in textvg:
            t.insert_n_curves(30)
        conclusion=TexText(r"$\cdot$每次计算时，余数小于除数\\",r"$\cdot$当余数出现循环时，结果出现循环").insert_n_curves(20).scale(0.75)
        conclusion[0].align_to(conclusion[1],LEFT)
        conclusion[0][0].scale(2).set_color(BLUE)
        conclusion[1][0].scale(2).set_color(BLUE)
        conclusion.to_corner(UL)
        self.add(conclusion)
        self.play(Write(textvg[0]))
        self.wait(0.5)
        frac=Tex("a","\\over","b")
        div=Tex("a","/","b")
        self.play(Write(frac))
        self.wait()
        self.play(TransformMatchingTex(frac,div))
        self.wait(0.5)
        self.play(div.animate.to_edge(UP),conclusion.animate.scale(0.75).to_corner(UL))
        self.wait()
        poly=RegularPolygon(9).scale(2.5)
        quosions=VGroup(*[Tex("q","_"+str(i)).scale(0.75).move_to(poly.get_vertices()[i-1]) for i in range(1,10)]).insert_n_curves(30)
        arrows=VGroup(*[Arrow(quosions[i].get_center(),quosions[i+1].get_center(),stroke_width=1).set_color(YELLOW) for i in range(len(quosions)-1)])
        brace=BraceText(poly,"至多$b$个余数",RIGHT)
        self.play(ShowCreationThenDestructionAround(conclusion[0]))
        self.wait(0.5)
        self.play(Write(textvg[1]),FadeOut(textvg[0]))
        self.wait(0.5)
        self.play(*[Write(q) for q in quosions])
        self.wait(0.5)
        self.play(brace.creation_anim(label_anim=Write))
        self.wait()
        self.play(Write(textvg[2]),FadeOut(textvg[1]))
        self.wait(0.5)
        self.play(LaggedStart(*[GrowArrow(arr) for arr in arrows],lag_ratio=0.5))
        self.wait()
        recusarrow=Arrow(quosions[-1].get_center(),quosions[4].get_center(),stroke_width=1).set_color(YELLOW)
        self.play(Write(textvg[3]),FadeOut(textvg[2]))
        self.wait(0.5)
        self.play(GrowArrow(recusarrow))
        anim=LaggedStart(*[Indicate(quosions[i]) for i in range(4,9)],lag_ratio=0.5,run_time=2)
        coor=poly.get_vertices()
        curve=Arc(start_angle=-PI/4*3,angle=300*DEGREES,radius=0.25,arc_center=(coor[4]+coor[5]+coor[6]+coor[7]+coor[8])/5,n_components=20,stroke_width=7).add_tip()
        self.play(anim,ShowCreation(curve,run_time=2))
        r=Tex(r"\overline{A.r_1r_2r_3r_4\dot{r_5}r_6r_7r_8\dot{r_9} }").to_edge(UP)
        self.wait()
        self.play(ShowCreationThenDestructionAround(conclusion[1]))
        self.wait(0.5)
        self.play(Write(textvg[4]),FadeOut(textvg[3]),FadeOut(div))
        self.wait(0.5)
        self.play(Write(r))
        self.wait()
        self.play(*[FadeOut(i) for i in [arrows,quosions,r,textvg[-1],conclusion,brace,recusarrow,curve]])
        self.wait()
        final=TexText("所有分数都能化成循环小数")
        self.play(Write(final))
        self.wait(2)